﻿Imports utilitiesLibrary

Public Class reserve
    Inherits System.Web.UI.Page
    Dim objDB As DBConnect
    Dim sql As String
    Dim ds As DataSet
    Dim count As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        objDB = Session("db")

        If Not IsPostBack Then
            sql = "SELECT AirportCode FROM AirportCode"
            ds = objDB.getDataSet(sql)
            ddlDest.DataSource = ds
            ddlOrigin.DataSource = ds
            ddlDest.DataTextField = "AirportCode"
            ddlDest.DataValueField = "AirportCode"
            ddlOrigin.DataValueField = "AirportCode"
            ddlOrigin.DataTextField = "AirportCode"
            ddlDest.DataBind()
            ddlOrigin.DataBind()
            lblResInfo.Visible = False
            btnAcptRes.Visible = False
        End If
    End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        Dim acctID As Integer
        Dim origin As String
        Dim dest As String
        Dim numSeats, i As Integer

        lblResInfo.Visible = True
        btnAcptRes.Visible = True

        acctID = txtAcctID.Text
        dest = ddlDest.SelectedValue
        origin = ddlOrigin.SelectedValue

        sql = "SELECT * FROM Flights f, Customers c WHERE f.OriginCode='" & origin & "' AND f.DestinationCode='" & dest & "' AND c.AccountID=" & acctID
        'MsgBox(sql)
        gvReserv.DataSource = objDB.getDataSet(sql)
        gvReserv.DataBind()

        For i = 0 To (gvReserv.Rows.Count - 1)
            numSeats = objDB.getField("SeatsAvailable", i)
            If numSeats = 0 Then
                gvReserv.Rows(i).Cells(6).Text = "No"
                gvReserv.Rows(i).Cells(7).Visible = False
            Else
                gvReserv.Rows(i).Cells(6).Text = "Yes"
            End If
        Next

    End Sub

    Protected Sub btnAcptRes_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAcptRes.Click
        Dim sql As String
        Dim seatNo As String
        Dim flightNo As Integer
        Dim acctID, i As Integer
        Dim seatBox As TextBox
        Dim oldSeats, newSeats As Integer
        Dim dist, newFF, oldFF As Integer

        acctID = txtAcctID.Text


        For i = 0 To (gvReserv.Rows.Count - 1)
            oldFF = 0
            newFF = 0
            dist = 0
            newSeats = 0
            oldSeats = 0
            seatBox = CType(gvReserv.Rows(i).FindControl("txtSeatNum"), TextBox)

            If (gvReserv.Rows(i).Cells(6).Text = "Yes" And Not seatBox.Text = String.Empty) Then

                'If (CType(gvReserv.Rows(i).Cells(7).Text, String) <> "" Or "Enter seat ID to reserve") Then
                flightNo = CType(gvReserv.Rows(i).Cells(4).Text, Integer)
                'seatBox = CType(gvReserv.Rows(i).FindControl("txtSeatNum"), TextBox)

                'insert seat reservation into FlightSeats table
                seatNo = seatBox.Text
                sql = "INSERT INTO FlightSeat VALUES (" & flightNo & ", " & acctID & ", '" & seatNo & "')"
                objDB.doUpdate(sql)

                'decrease reserverd flights SeatsAvailable by 1
                sql = "SELECT * FROM Flights WHERE FlightNumber=" & flightNo
                'MsgBox(sql)
                objDB.getDataSet(sql)
                oldSeats = (objDB.getField("SeatsAvailable", 0))
                If (oldSeats > 0) Then
                    newSeats = oldSeats - 1
                    sql = "UPDATE Flights SET SeatsAvailable=" & newSeats & " WHERE FlightNumber=" & flightNo
                    'MsgBox(sql)
                    objDB.doUpdate(sql)
                End If

                'add FF miles
                sql = "SELECT Distance FROM Flights WHERE FlightNumber=" & flightNo
                objDB.getDataSet(sql)
                dist = objDB.getField("Distance", 0)

                sql = "SELECT FFMiles FROM FreqFlier WHERE AccountID=" & acctID
                objDB.getDataSet(sql)
                oldFF = objDB.getField("FFMiles", 0)

                newFF = oldFF + dist
                sql = "UPDATE FreqFlier SET FFMiles=" & newFF & " WHERE AccountID=" & acctID
                objDB.doUpdate(sql)

                Response.Redirect("reservations.aspx")

            End If

        Next


    End Sub
End Class